import scrapy
from ..items import InstitutionsItem


class InstitutionSpider(scrapy.Spider):
    name = "institution"
    allowed_domains = ["www.apta.gov.cn"]
    # 阜阳
    start_urls = ["http://www.apta.gov.cn/Officer_PositionView?examid=329&&type=06&&pi=" + str(x) for x in range(1, 43)]

    # 省直
    # start_urls = ["http://www.apta.gov.cn/Officer_PositionView?examid=329&&type=30&&pi=" + str(x) for x in range(1, 68)]

    def parse(self, response):
        item = InstitutionsItem()
        trs = response.xpath("/html/body/table/tr/td/table/tr[3]/td/table/tr")
        for i, tr in enumerate(trs):
            if i > 0:
                tds = tr.xpath("td")
                item['job_code'] = tds.xpath("text()").extract()[0].strip()
                item['manage_people'] = tds.xpath("text()").extract()[1].strip()
                item['manager_name'] = tds.xpath("text()").extract()[2].strip()
                item['plan_num'] = tds.xpath("text()").extract()[3].strip()
                item['plan_department'] = tds.xpath("text()").extract()[4].strip()
                item['specialized'] = tds.xpath("text()").extract()[5].strip()
                item['academic_qualifications'] = tds.xpath("text()").extract()[6].strip()
                item['degree'] = tds.xpath("text()").extract()[7].strip()
                item['age'] = tds.xpath("text()").extract()[8].strip()
                item['other'] = tds.xpath("text()").extract()[9].strip()
                item['common_categories'] = tds.xpath("text()").extract()[10].strip()
                item['common_code'] = tds.xpath("text()").extract()[11].strip()
                item['remark'] = tds.xpath("text()").extract()[12].strip()
                item['phone'] = tds.xpath("text()").extract()[13].strip()
                item['key_word'] = '阜阳_test'
                yield item
        pass
